package com.gyc.simple.sysuser.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;


public class GetAuthorityResources{
    //
    private ApplicationContext applicationContext;

    private Map<String,String> keyValues = new HashMap<>();

    public GetAuthorityResources(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
        setKeyValues();
    }

    Map<String, String> getKeyValues() {
        return keyValues;
    }

    private void setKeyValues(){
        try {
            Map<String,Object> beansWithAnnotationMap = this.applicationContext.getBeansWithAnnotation(RestController.class);
            Class<? extends Object> clazz ;
            for (Map.Entry<String,Object> bean:beansWithAnnotationMap.entrySet()){
                clazz = bean.getValue().getClass();
                Method[] methods = clazz.getMethods();
                for(Method method:methods){
                    if(method.isAnnotationPresent(AuthorityResources.class)){
                        AuthorityResources annotation = (AuthorityResources)method.getAnnotation(AuthorityResources.class);
                        keyValues.put(annotation.url(),annotation.desc());
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
